package top.fengleifeng.question;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author : fengleifeng
 * @Package top.fengleifeng.question
 * @Description:
 * @date Date : 2020年11月25日 4:10 下午
 */
public class Num1657确定两个字符串是否接近 {

    class Solution {

        public boolean closeStrings(String word1, String word2) {
            char[] oneChars = word1.toCharArray();
            char[] twoChars = word2.toCharArray();

            Arrays.sort(oneChars);
            Arrays.sort(twoChars);
            String one = new String(oneChars);
            String two = new String(twoChars);
            if (one.equals(two)) {
                return true;
            }
            //获取个数
            int[] oneintArr = new int[26];
            int[] twointArr = new int[26];
            for (int i = 0; i < oneChars.length; i++) {
                oneintArr[oneChars[i] - 'a']++;
            }
            for (int i = 0; i < twoChars.length; i++) {
                twointArr[twoChars[i] - 'a']++;
            }
            for (int i = 0; i < oneintArr.length; i++) {
                if(oneintArr[i]==0 && twointArr[i]!=0){
                    return false;
                }
                if(twointArr[i]==0 && oneintArr[i]!=0){
                    return false;
                }
            }


            Map<Integer, Integer> map = new HashMap<>();
            Map<Integer, Integer> map2 = new HashMap<>();
            for (int i = 0; i < oneintArr.length; i++) {
                if (map.containsKey(oneintArr[i])) {
                    map.put(oneintArr[i], map.get(oneintArr[i]) + 1);
                } else {
                    map.put(oneintArr[i], 1);
                }
            }

            for (int i = 0; i < twointArr.length; i++) {
                if (map2.containsKey(twointArr[i])) {
                    map2.put(twointArr[i], map2.get(twointArr[i]) + 1);
                } else {
                    map2.put(twointArr[i], 1);
                }
            }

            Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry<Integer, Integer> next = iterator.next();
                Integer key = next.getKey();
                Integer value = next.getValue();
                if (map2.containsKey(key)) {
                    if (map2.get(key).intValue() != value.intValue()) {
                            return false;
                    }
                } else {
                    return false;
                }
            }
            Iterator<Map.Entry<Integer, Integer>> iterator2 = map2.entrySet().iterator();
            while (iterator2.hasNext()) {
                Entry<Integer, Integer> next = iterator2.next();
                Integer key = next.getKey();
                Integer value = next.getValue();
                if (map.containsKey(key)) {
                    if (map.get(key).intValue() != value.intValue()) {
                        return false;
                    }
                } else {
                    return false;
                }
            }

            return true;
        }
    }

    public static void main(String[] args) {
//        String s = "abc";
//        String s2 = "bac";
//        String s = "cabbba";
//        String s2 = "abbccc";
                String s = "uau";
        String s2 = "sxs";
        boolean b = new Num1657确定两个字符串是否接近().new Solution().closeStrings(s, s2);
        System.out.println(b);
    }
}
